package me.zhanshi123.vipsystem.feature.vipjoin;

import cn.nukkit.Player;
import me.zhanshi123.vipsystem.Main;

import java.text.MessageFormat;
import java.util.List;

public class VipJoinAction {
    private List<String> join;
    private List<String> leave;


    public VipJoinAction(List<String> join, List<String> leave) {
        this.join = join;
        this.leave = leave;
    }

    public List<String> getJoin() {
        return join;
    }

    public void setJoin(List<String> join) {
        this.join = join;
    }

    public List<String> getLeave() {
        return leave;
    }

    public void setLeave(List<String> leave) {
        this.leave = leave;
    }

    public void performJoinAction(Player player) {
        perform(join, player);
    }

    public void performLeaveAction(Player player) {
        perform(leave, player);
    }

    private void perform(List<String> commands, Player player) {
        commands.stream()
                .filter(str -> str.startsWith("[Broadcast]"))
                .map(str -> str.replace("[Broadcast]", "").replace("&", "§").trim())
                .forEach(str -> Main.getInstance().getServer().broadcastMessage(MessageFormat.format(str, player.getName())));
        commands.stream()
                .filter(str -> str.startsWith("[Console]"))
                .map(str -> str.replace("[Console]", "").replace("&", "§").trim())
                .forEach(str -> Main.getInstance().getServer().dispatchCommand(Main.getInstance().getServer().getConsoleSender(), MessageFormat.format(str, player.getName())));
        commands.stream()
                .filter(str -> str.startsWith("[Message]"))
                .map(str -> str.replace("[Message]", "").replace("&", "§").trim())
                .forEach(str -> player.sendMessage(MessageFormat.format(str, player.getName())));
    }
}
